package com.genesis.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.genesis.mall.common.PmsProductPage;
import com.genesis.mall.common.Result;
import com.genesis.mall.entity.PmsProduct;
import com.genesis.mall.service.IPmsProductService;
import org.apache.commons.lang3.StringUtils;
import org.mockito.internal.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.genesis.mall.dto.PmsProductDto;
import com.genesis.mall.entity.*;
import com.genesis.mall.service.*;
import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品信息 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
//// TODO 前后台冲突
//@RestController
//@RequestMapping("/product")
@Slf4j
public class PmsProductController {

    @Autowired
    private IPmsProductService pmsProductService;

    /**
     * 新品新增商品展示
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
//    @GetMapping("/list")
    public Result<PmsProductPage<PmsProduct>>list(Long pageNum,Long pageSize,String keyword){
        Page<PmsProduct> pageInfo = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<PmsProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(keyword),PmsProduct::getName,keyword);
        queryWrapper.orderByDesc(PmsProduct::getOriginalPrice);
        pmsProductService.page(pageInfo,queryWrapper);
        PmsProductPage<PmsProduct> productPage = new PmsProductPage<>();
        BeanUtils.copyProperties(pageInfo,productPage);
        return Result.success(productPage);
    }



    /**
     * 分页查询商品信息
     * @return
     */
    @GetMapping("/list")
    public Result<PmsProductPage<PmsProduct>> getPage(int pageNum, int pageSize,String keyword, String productSn,
                                                      Integer publishStatus,Integer verifyStatus,Integer productCategoryId,Integer brandId){
        //分页构造器
        Page<PmsProduct> pageInfo = new Page<>(pageNum,pageSize);
        //条件构造器
        LambdaQueryWrapper<PmsProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(keyword),PmsProduct::getName,keyword)
                .like(StringUtils.isNotEmpty(productSn),PmsProduct::getProductSn,productSn)
                .eq(publishStatus!=null,PmsProduct::getPublishStatus,publishStatus)
                .eq(verifyStatus!=null,PmsProduct::getVerifyStatus,verifyStatus)
                .eq(productCategoryId!=null,PmsProduct::getProductCategoryId,productCategoryId)
                .eq(brandId!=null,PmsProduct::getBrandId,brandId);

        pmsProductService.page(pageInfo,queryWrapper);
        PmsProductPage<PmsProduct> pmsProductPage = new PmsProductPage();
        BeanUtils.copyProperties(pageInfo,pmsProductPage);
        return Result.success(pmsProductPage);
    }

    /**
     * 修改商品上下架状态
     * @param ids
     * @param publishStatus
     * @return
     */
    @PostMapping("/update/publishStatus")
    public Result<String> publishStatus(@RequestParam List<Integer> ids, int publishStatus){
        log.info("修改商品上下架状态");
//        System.out.println(ids);
//        for (Integer id : ids) {
//            PmsProduct pmsProduct = iPmsProductService.getById(id);
//            pmsProduct.setPublishStatus(publishStatus);
//            iPmsProductService.updateById(pmsProduct);
//        }

        List<PmsProduct> pmsProducts = ids.stream().map(id -> {
            PmsProduct product = pmsProductService.getById(id);
            product.setPublishStatus(publishStatus);
            return product;
        }).collect(Collectors.toList());
        pmsProductService.updateBatchById(pmsProducts);
        return Result.success("修改成功");
    }

    /**
     * 修改新品状态
     * @param ids
     * @param newStatus
     * @return
     */
    @PostMapping("/update/newStatus")
    public Result<String> newStatus(@RequestParam List<Integer> ids, int newStatus){
        log.info("修改新品状态");

        for (Integer id : ids) {
            PmsProduct pmsProduct = pmsProductService.getById(id);
            pmsProduct.setNewStatus(newStatus);
            pmsProductService.updateById(pmsProduct);
        }

        return Result.success("新品状态修改成功");
    }

    /**
     * 修改推荐商品状态
     * @param ids
     * @param recommendStatus
     * @return
     */
    @PostMapping("/update/recommendStatus")
    public Result<String> recommendStatus(@RequestParam List<Integer> ids, int recommendStatus){
        log.info("修改推荐商品状态");

        for (Integer id : ids) {
            PmsProduct product = pmsProductService.getById(id);
            product.setRecommandStatus(recommendStatus);
            pmsProductService.updateById(product);
        }

        return Result.success("推荐商品状态修改成功");
    }

    /**
     * 商品移入回收站
     * @param ids
     * @param deleteStatus
     * @return
     */
    @PostMapping("/update/deleteStatus")
    public Result<String> deleteStatus(@RequestParam List<Integer> ids, int deleteStatus){
        log.info("商品移入回收站");

        for (Integer id : ids) {
            PmsProduct product = pmsProductService.getById(id);
            product.setDeleteStatus(deleteStatus);
            pmsProductService.updateById(product);
            //根据id删除商品
            pmsProductService.removeById(id);
        }

        return Result.success("商品移入回收站成功");
    }


    /**
     * 添加商品
     * @param pmsProductDto
     * @return
     */
    @Autowired
    private IPmsMemberPriceService pmsMemberPriceService;

    @Autowired
    private ICmsSubjectProductRelationService cmsSubjectProductRelationService;
    @Autowired
    private IPmsSkuStockService pmsSkuStockService;
    @Autowired
    private IPmsProductLadderService pmsProductLadderService;
    @Autowired
    private IPmsProductFullReductionService pmsProductFullReductionService;
    @Autowired
    private IPmsProductAttributeValueService pmsProductAttributeValueService;
    @Autowired
    private ICmsPrefrenceAreaProductRelationService cmsPrefrenceAreaProductRelationService;
    @PostMapping("/create")
    public Result<?> craete(@RequestBody PmsProductDto pmsProductDto){
        log.info(pmsProductDto.toString());
        for (PmsMemberPrice pmsMemberPrice : pmsProductDto.getMemberPriceList()) {
            pmsMemberPriceService.save(pmsMemberPrice);
        }
        pmsProductService.save(pmsProductDto);
        for (CmsSubjectProductRelation cmsSubjectProductRelation : pmsProductDto.getSubjectProductRelationList()) {
            cmsSubjectProductRelationService.save(cmsSubjectProductRelation);
        }
        for (PmsSkuStock pmsSkuStock : pmsProductDto.getSkuStockList()) {
            pmsSkuStockService.save(pmsSkuStock);
        }
        for (PmsProductLadder pmsProductLadder : pmsProductDto.getProductLadderList()) {
            pmsProductLadderService.save(pmsProductLadder);
        }
        for (PmsProductFullReduction pmsProductFullReduction : pmsProductDto.getProductFullReductionList()) {
            pmsProductFullReductionService.save(pmsProductFullReduction);
        }
        for (PmsProductAttributeValue pmsProductAttributeValue : pmsProductDto.getProductAttributeValueList()) {
            pmsProductAttributeValueService.save(pmsProductAttributeValue);
        }
        for (CmsPrefrenceAreaProductRelation cmsPrefrenceAreaProductRelation : pmsProductDto.getPrefrenceAreaProductRelationList()) {
            cmsPrefrenceAreaProductRelationService.save(cmsPrefrenceAreaProductRelation);
        }

        return Result.success("商品添加成功");
    }

    /**
     * 根据id回显商品信息
     * @param id
     * @return
     */
    @GetMapping("/updateInfo/{id}")
    public Result<PmsProductDto> updateInfo(@PathVariable int id){
        log.info("根据id回显商品信息");

        PmsMemberPrice memberPrice = pmsMemberPriceService.getById(id);
        //商品信息
        PmsProduct pmsProduct = pmsProductService.getById(id);
        CmsSubjectProductRelation productRelation1 = cmsSubjectProductRelationService.getById(id);
        PmsSkuStock skuStock = pmsSkuStockService.getById(id);
        PmsProductLadder productLadder = pmsProductLadderService.getById(id);
        PmsProductFullReduction fullReduction = pmsProductFullReductionService.getById(id);
        PmsProductAttributeValue attributeValue = pmsProductAttributeValueService.getById(id);
        CmsPrefrenceAreaProductRelation productRelation2 = cmsPrefrenceAreaProductRelationService.getById(id);

        PmsProductDto pmsProductDto = new PmsProductDto();

        List<PmsMemberPrice> memberPriceList = new ArrayList<>();
        List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList = new ArrayList<>();
        List<PmsProductAttributeValue> productAttributeValueList = new ArrayList<>();
        List<PmsProductFullReduction> productFullReductionList = new ArrayList<>();
        List<PmsProductLadder> productLadderList = new ArrayList<>();
        List<PmsSkuStock> skuStockList = new ArrayList<>();
        List<CmsSubjectProductRelation> subjectProductRelationList = new ArrayList<>();

        memberPriceList.add(memberPrice);
        prefrenceAreaProductRelationList.add(productRelation2);
        productAttributeValueList.add(attributeValue);
        productFullReductionList.add(fullReduction);
        productLadderList.add(productLadder);
        skuStockList.add(skuStock);
        subjectProductRelationList.add(productRelation1);

        BeanUtils.copyProperties(pmsProduct,pmsProductDto);
        pmsProductDto.setMemberPriceList(memberPriceList);
        pmsProductDto.setSubjectProductRelationList(subjectProductRelationList);
        pmsProductDto.setSkuStockList(skuStockList);
        pmsProductDto.setProductLadderList(productLadderList);
        pmsProductDto.setProductFullReductionList(productFullReductionList);
        pmsProductDto.setProductAttributeValueList(productAttributeValueList);
        pmsProductDto.setPrefrenceAreaProductRelationList(prefrenceAreaProductRelationList);

        return Result.success(pmsProductDto);
    }

    /**
     * 修改商品信息
     * @param id
     * @param pmsProductDto
     * @return
     */
    @PostMapping("/update/{id}")
    public Result<String> update(@PathVariable int id,@RequestBody PmsProductDto pmsProductDto){
        log.info("修改商品信息");

        for (PmsMemberPrice pmsMemberPrice : pmsProductDto.getMemberPriceList()) {
            if (pmsMemberPrice != null) {
                pmsMemberPriceService.updateById(pmsMemberPrice);
            }
        }
        pmsProductService.updateById(pmsProductDto);
        for (CmsSubjectProductRelation cmsSubjectProductRelation : pmsProductDto.getSubjectProductRelationList()) {
            if(cmsSubjectProductRelation != null){
                cmsSubjectProductRelationService.updateById(cmsSubjectProductRelation);
            }
        }
        for (PmsSkuStock pmsSkuStock : pmsProductDto.getSkuStockList()) {
            if (pmsSkuStock != null) {
                pmsSkuStockService.updateById(pmsSkuStock);
            }
        }
        for (PmsProductLadder pmsProductLadder : pmsProductDto.getProductLadderList()) {
            if (pmsProductLadder != null) {
                pmsProductLadderService.updateById(pmsProductLadder);
            }
        }
        for (PmsProductFullReduction pmsProductFullReduction : pmsProductDto.getProductFullReductionList()) {
            if (pmsProductFullReduction != null) {
                pmsProductFullReductionService.updateById(pmsProductFullReduction);
            }
        }
        for (PmsProductAttributeValue pmsProductAttributeValue : pmsProductDto.getProductAttributeValueList()) {
            if (pmsProductAttributeValue != null) {
                pmsProductAttributeValueService.updateById(pmsProductAttributeValue);
            }
        }
        for (CmsPrefrenceAreaProductRelation cmsPrefrenceAreaProductRelation : pmsProductDto.getPrefrenceAreaProductRelationList()) {
            if (cmsPrefrenceAreaProductRelation != null) {
                cmsPrefrenceAreaProductRelationService.updateById(cmsPrefrenceAreaProductRelation);
            }
        }

        return Result.success("修改成功");
    }
}
